package com.mp3player.mp3player;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import com.mp3player.model.Mp3Info;
import com.mp3player.service.PlayMp3Service;
import com.mp3player.util.MetaData;
import com.mp3player.util.ToolUtils;

public class PlayerActivity extends AppCompatActivity {

    private Mp3Info mp3Info = null;

    private BroadcastReceiver playStatusReceiver = null;
    private BroadcastReceiver lrcMessageReceiver = null;
    private BroadcastReceiver playPositionReceiver = null;
    IntentFilter intentFilterLrcMessage = null;

    private ImageButton dynamicButton = null;
    private TextView lyric_1TextView = null;
    private TextView lyric0TextView = null;
    private TextView lyric1TextView = null;
    private TextView playTimeTextView = null;
    private SeekBar seekBar = null;

    private boolean shouldChangeSeekBar = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);

        //获得传过来的Intent
        Intent intent = this.getIntent();
        if (intent != null) {
            mp3Info = (Mp3Info) intent.getSerializableExtra(MetaData.MP3_INFO_KEY);
        }

        //注册播放状态接收器
        playStatusReceiver = new PlayStatusReceiver();
        IntentFilter intentFilter = new IntentFilter(MetaData.PLAY_STATUS_ACTION);
        this.registerReceiver(playStatusReceiver, intentFilter);

        //声明歌词信息接收器
        lrcMessageReceiver = new LrcMessageReceiver();
        intentFilterLrcMessage = new IntentFilter(MetaData.UPDATE_LYRIC_ACTION);

        //启动mp3播放服务
        Intent i = new Intent();
        i.setClass(this, PlayMp3Service.class);
        i.putExtra(MetaData.INTENT_CATEGORY, MetaData.START_SERVICE);
        i.putExtra(MetaData.MP3_INFO_KEY, mp3Info);
        this.startService(i);

        //设置当前曲目名字
        TextView current_mp3_name = (TextView) this.findViewById(R.id.current_mp3_name);
        current_mp3_name.setText(mp3Info.getMp3Name());

        //获取按钮并添加监听器
        dynamicButton = (ImageButton) this.findViewById(R.id.dynamic_btn);
        dynamicButton.setOnClickListener(new DynamicButtonOnClickListener());

        //获取各个控件
        lyric0TextView = (TextView) this.findViewById(R.id.lyric0);
        lyric_1TextView = (TextView) this.findViewById(R.id.lyric_1);
        lyric1TextView = (TextView) this.findViewById(R.id.lyric1);
        playTimeTextView = (TextView) findViewById(R.id.play_time);
        seekBar = (SeekBar) findViewById(R.id.seek_bar);

        //声明歌曲播放位置以及总时长的接收器
        playPositionReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (shouldChangeSeekBar) {
                    seekBar.setMax(intent.getIntExtra(MetaData.TOTAL_PLAY_TIME_KEY, 60000));
                    seekBar.setProgress(intent.getIntExtra(MetaData.PLAY_POSITION_KEY, 1));
                }
            }
        };
        this.registerReceiver(playPositionReceiver, new IntentFilter(MetaData.PLAY_POSITION_ACTION));

        //SeekBar绑定监听器
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                playTimeTextView.setText(ToolUtils.milliseconds2TimeString(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                shouldChangeSeekBar = false;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                shouldChangeSeekBar = true;
                Intent intent = new Intent(MetaData.SEEK_POSITION_ACTION);
                intent.putExtra(MetaData.SEEK_POSITION_KEY, seekBar.getProgress());
                PlayerActivity.this.sendBroadcast(intent);
            }
        });
    }

    @Override
    protected void onStop() {
        Intent i = new Intent(MetaData.COMMAND_FOR_PLAY_SERVICE_ACTION);
        i.putExtra(MetaData.STOP_COMMAND_KEY, MetaData.STOP_SERVICE_COMMAND);
        this.sendBroadcast(i);
        this.unregisterReceiver(playStatusReceiver);
        this.unregisterReceiver(lrcMessageReceiver);
        super.onStop();
    }

    class DynamicButtonOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(PlayerActivity.this, PlayMp3Service.class);
            intent.putExtra(MetaData.INTENT_CATEGORY, MetaData.SWITCH_MP3_STATUS);
            PlayerActivity.this.startService(intent);
        }
    }

    class PlayStatusReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (MetaData.START_PLAYING == intent.getIntExtra(MetaData.PLAYING_STATUS_KEY, 0)) {
                dynamicButton.setImageResource(R.drawable.pause);
                PlayerActivity.this.registerReceiver(lrcMessageReceiver, intentFilterLrcMessage);
            }
            if (MetaData.PAUSE_PLAYING == intent.getIntExtra(MetaData.PLAYING_STATUS_KEY, 0)) {
                dynamicButton.setImageResource(R.drawable.play);
                try {
                    PlayerActivity.this.unregisterReceiver(lrcMessageReceiver);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class LrcMessageReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            lyric_1TextView.setText(intent.getCharSequenceExtra(MetaData.LAST_LYRIC_KEY));
            lyric0TextView.setText(intent.getCharSequenceExtra(MetaData.UPDATE_LYRIC_KEY));
            lyric1TextView.setText(intent.getCharSequenceExtra(MetaData.NEXT_LYRIC_KEY));
        }
    }

}
